<?php

/**
 * Botenmarkt Front-end
 * 
 * DomainObjects
 * 
 * News domainObject class File
 *
 * @category    botenmarkt_front
 * @package     library
 * @subpackage  domainObjects
 * 
 * @author      valerii lazarevych <valeriy.fremar@gmail.com>
 * @copyright   fremar ltd.
 * 
 * @license     default
 * @version     12:45:21 PM | Jan 24, 2012
 */

/**
 * News domainObject class
 * 
 * Represent OOP News entity 
 * with this hold functionals to working with news data
 */
class   News_domainObject        extends     domainObject
{
    /**
     * News title
     * 
     * @var string
     */
    public      $title             =       null;
    
    /**
     * News preview description
     * 
     * @var string
     */
    public      $shortDescr        =       null;
    
    /**
     * News message
     * 
     * @var string
     */
    public      $description       =       null;    
    
    /**
     * News picture url
     * 
     * @var string  |   url
     */
    public      $picture_url       =       null;
    
    /**
     * Publishing online status of the news
     * 
     * @var bool
     */
    public      $newsStatus        =       null;
    
    /**
     * News publish date
     * 
     * @var string |  d-m-Y H:i:s
     */
    public      $publishDate       =       null;
        
    /**
     * News blank picture name
     * 
     * @var string 
     */
    private static $newsBlankPic      =       'blank.png';
                     
    
    /**
     * Get news title
     * 
     * @return string
     */
    public function newsTitle()
    {
        if (!$this->title)            
             $this->title    =   implode(' ', array_slice( str_word_count($this->newsPreview(), '2'), 0, 3 ) );
        
        return html_entity_decode($this->title);
    }
    
    /**
     * Get news preview description
     * 
     * @return string
     */
    public function newsPreview()
    {        
        if (!$this->shortDescr)
             $this->shortDescr  =  implode(' ', array_slice( str_word_count($this->newsMessage(), '2'), 0, 30 ) );
        
        return html_entity_decode($this->shortDescr);
    }
    /**
     * Get news full message
     * 
     * @return string
     */
    public function newsMessage()
    {
        if (!$this->description)
             $this->description     =   $this->shortDescr  ?: $this->title;
        
        return html_entity_decode($this->description);
    }
    
    /**
     * Get news picture html entity
     * 
     * @return string   |   url
     */
    public function newsPicture()        
    {
        if (!$this->picture_url)
             $this->picture_url      =   self::$blankNewsPic;
        
        return '<img src="'.IMGS_PATH.'news/'.$this->picture_url.'" alt="'.$this->newsTitle().'" />';
    }
    
    /**
     * Validate & get news link
     * 
     * @todo add link validation
     * 
     * @param string $linkBody 
     * @return string
     */    
    public function newsLink($linkBody = '')
    {
        return '<a href="'.ENTRYPOINT_URL.'?cmd=newsdetail?id='.$this->getId().'">'.
                     substr($this->newsTitle(), 0, 30).
                    (strlen($this->newsTitle()) <= 30  ? "" : "...") . '</a>';
    }
    
    public function newsPublishDate($format = 'd-m-Y')
    {
        return date($format, strtotime($this->publishDate) );
    }
        
}

?>
